#***********************************************************
#* Copyright (c) 2006, Zhimin Zhan.
#* Distributed open-source, see full license in MIT-LICENSE
#***********************************************************

module RWebUnit
  module Utils

    #useful methods for test data
    def getToday_AU()
	  format_date(Time.now, "%02d/%02d/%04d")
    end
    alias getToday getToday_AU

    def getToday_US()
	  sprintf("%02d/%02d/%04d", Time.now.month, Time.now.day, Time.now.year)
    end

    def getDaysBefore(days)
        nil if !(days.instance_of?(Fixnum))
	    format_date(Time.now - days * 24 * 3600)
    end

    def getYesterday
	    getDaysBefore(1)
    end
    
    def getDaysAfter(days)
	    nil if !(days.instance_of?(Fixnum))
	    format_date(Time.now + days * 24 * 3600)
    end
    
    def getTomorrow
	    getDaysAfter(1)
    end
    
    #TODO: getDaysBefore, getDaysAfter, getYesterday, getTomorrow

    # return a random number >= min, but <= max
    def randomNumber(min, max)
      rand(max-min+1)+min
    end

    def randomBoolean()
      return randomNumber(0, 1) == 1
    end

    def randomChar(lowercase)
      sprintf("%c", random_number(97, 122)) if lowercase
      sprintf("%c", random_number(65, 90)) unless lowercase
    end

    def randomDigit()
      sprintf("%c", randomNumber(48, 57))
    end

    def randomString(length, lowercase)
      randomStr = ""
      length.times {
        randomStr += randomChar(lowercase)
      }
      randomStr
    end

    # Return a random string in a rangeof pre-defined strings
    def randomStringInCollection(arr)
      return nil if arr.empty?
      index = random_number(0, arr.length-1)
      arr[index]
    end

private 
    def format_date(date, date_format = nil)
	  date_format ||=  "%02d/%02d/%04d"
	  sprintf(date_format, date.day, date.month, date.year)
    end
    
    
  end
end
